package com.kkb.controller;

import com.kkb.entity.User;
import com.kkb.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;

@Controller
public class LoginController {
    @Autowired
    private LoginService loginService;

    @RequestMapping("/login.do")
    public String login(
            @RequestParam("username") String username,
            @RequestParam("password") String password,
            Model model,
            HttpServletRequest hreq
            ){
        User user = loginService.login(username, password);
        if (user != null){
            hreq.getSession().setAttribute("user", user);
            model.addAttribute("user", user);
        }
        return "/home";
    }
}
